<?php
//代理商列表
namespace app\ausadmin\controller;
use think\Request;
use think\Db;
use app\common\logic\Agentlogic;
use app\common\logic\Upload;

class Agent extends Common
{
    private $agentLogic;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->agentLogic = new Agentlogic();
    }

    /**
     *代理商列表
     *
     */
	public function agentlist(){
		$pages = 20;
		//定义where条件
		$where = 'type=1';

		$pageParam    = ['query' =>[]];
		//判断是否有按代理商名称查询
		if(!empty(input('agent_name'))){
			$where .= ' AND agent_name LIKE '."'%".input('agent_name')."%' ";
			$this->assign('agent_name',input('agent_name'));
		}
		
		//判断是否按电话号码查询
		if(!empty(input('phone'))){
			$where .= ' AND phone = '.input('phone').' ';
			$this->assign('phone',input('phone'));
		}

        if(!empty(input('status'))){
			$where .= ' AND is_use = '.input('status').' ';
		    $this->assign('is_use',input('status'));
		}
		
		$list = Db::name('agent')
				->where($where)
				->field('id,agent_name,real_name,phone,rate,status,addtime,is_use')
				->order('addtime desc')
				->paginate($pages, false, $pageParam);

        $this->assign('status', $this->agentLogic->getStatus());
		$this->assign('list',$list);
		
		return $this->fetch();
	}

	public function add(){
		if ( Request::instance()->isPost()) {
		    $data = input();
		    $result = $this->agentLogic->add($data);
		    return $result;
		}
		$status = $this->agentLogic->getStatus();
		$this->assign('status', $status);
		return $this->fetch();
	}

	public function edit($id='',$data= ''){
		
		if ( Request::instance()->isPost()) {
            $data = input();
            $result = $this->agentLogic->edit($data);
            return $result;
		}else{
			$id =  Request::instance()->get('id','','number_int');
			if(!empty($id)){
				$where = 'id = '.$id;
				$jquery = Db::name('agent')->where($where)->find();

                $init_image = [];
                $con = [];
                $card_pic = '';
                if(!empty($jquery['card_pic'])){
                    $init_image = trim($jquery['card_pic'], ',');
                    $img_list = explode(',', $init_image);
                    $all_image = [];
                    $url = config('txyunview');
                    foreach ($img_list as $img) {
                        $name = explode('/', $img);
                        $name = array_pop($name);
                        $all_image[] = ['name' => $name, 'url' => $url.$img];
                        $card_pic .= $url.$img.',';
                    }
                    $card_pic = trim($card_pic, ',');
                } else {
                    $all_image = [];
                }
                $this->assign('card_pic', $card_pic);
                $this->assign('all_image', json_encode($all_image));
                $this->assign('status', $this->agentLogic->getStatus());
				$this->assign('init_image',json_encode($init_image));
				$this->assign('con',json_encode($con));
				$this->assign('v',$jquery);
				return $this->fetch();
			}else{
				die('非法访问!');
			}
			
		}
		
	}

	public function edit_employee($id = '', $data = [])
    {
        if ( Request::instance()->isPost()) {
            $data = input();
            $result = $this->agentLogic->editEmployee($data);
            return $result;
        }else{
            $id =  Request::instance()->get('id','','number_int');
            if(!empty($id)){
                $where = 'id = '.$id;
                $jquery = Db::name('agent')->where($where)->find();

                $init_image = [];
                $con = [];
                $card_pic = '';
                if(!empty($jquery['card_pic'])){
                    $init_image = trim($jquery['card_pic'], ',');
                    $img_list = explode(',', $init_image);
                    $all_image = [];
                    $url = config('txyunview');
                    foreach ($img_list as $img) {
                        $name = explode('/', $img);
                        $name = array_pop($name);
                        $all_image[] = ['name' => $name, 'url' => $url.$img];
                        $card_pic .= $url.$img.',';
                    }
                    $card_pic = trim($card_pic, ',');
                } else {
                    $all_image = [];
                }
                $this->assign('card_pic', $card_pic);
                $this->assign('all_image', json_encode($all_image));
                $this->assign('status', $this->agentLogic->getStatus());
                $this->assign('con',json_encode($con));
                $this->assign('info',$jquery);
                $agent = Db::name('agent')->where("is_use=1 and type=1")->field('id,agent_name')->select();

                $this->assign('action', 'edit_employee');
                $this->assign('agent',$agent);
                return $this->fetch('add_employee');
            }else{
                die('非法访问!');
            }

        }
    }
	public function delete($id=''){
		$id =  Request::instance()->get('id','','number_int');
		$data['id'] = $id;
		$data['is_delete'] = 0; 
		if(Db::name( 'seller')->update($data)){
    			//$this->redirect('index');
    		$ret = Db::name('charecabinet')->where('sid',$id)->find();
    		Db::name('charecabinet')->where('id',$ret['id'])->setField('sid', 0);
    		$oid = session('oid');$m = 'ausadmin'; $c = 'Seller'; $action="delete"; $uid=session('adminid'); $querystring = '删除商户 '.$id.' 信息';$ip = get_ip();$os = get_os();
                    set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
    		return ['code'=>0, 'msg'=>'删除成功！'];
		}else{
			return ['code'=>-1050, 'msg'=>'删除失败！'];
		}

	}

	/**
	 *更改状态
	 *
	 */
	public function change_status($id = '')
    {
        return $this->agentLogic->changeStatus($id);
    }

     //递推列表
	public function employeelist(){
		//定义每页显示记录数
		$pages = 20;
		//定义where条件
		$where = 'type=2';
		$pageParam    = ['query' =>[]];
		//判断是否有按代理商名称查询
        $real_name = trim(input('real_name'));
		if(!empty($real_name)){
			$where .= ' AND real_name LIKE '."'%".$real_name."%' ";
			$this->assign('real_name',$real_name);
		}
		
		//判断是否按电话号码查询
        $phone = trim(input('phone'));
		if(!empty($phone)){
			$where .= ' AND phone = '.$phone.' ';
			$this->assign('phone',$phone);
		}

		//判断是否按代理商ID查询
		if(!empty(input('belong_agent'))){
			$where .= ' AND belong_agent = '.input('belong_agent').' ';
			$this->assign('belong_agent',input('belong_agent'));
		}

        if(!empty(input('status'))){
			$where .= ' AND is_use = '.input('status').' ';
		    $this->assign('is_use',input('status'));
		}

		$list = Db::name('agent')
				->where($where)
				->field('id,agent_name,real_name,phone,is_use,addtime,belong_agent,card_sn')
				->order('id desc')
				->paginate($pages, false, $pageParam);
		$this->assign('list',$list);

        // 获取所有代理商
        $agent = Db::name('agent')->where("is_use=1 and type=1")->field('id,agent_name')->select();
        $agent = array_column($agent, 'agent_name', 'id');
        $this->assign('status', $this->agentLogic->getStatus());
        $this->assign('agent',$agent);
		return $this->fetch();
	}

    public function employeedetail($id=''){
        $status = [1=>"待审核",2=>"审核通过",3=>"审核不通过"];
		$id =  Request::instance()->get('id','','number_int');
		if(!empty($id)){
			$where = 'id = '.$id;
			$jquery = Db::name('agent')->where($where)->find();
			$this->assign('v',$jquery);
            // 获取所有代理商
            $agentTmp = Db::name('agent')->where("is_use=1 and type=1")->field('id,agent_name')->select();
            $agent = [];
            foreach($agentTmp as $val){
                $agent[$val['id']] = $val;
            }
            $this->assign('agent',$agent);
            $this->assign('status',$status);
			return $this->fetch();
		}else{
			die('非法访问!');
		}
	}

    // 添加地推
    public function add_employee(){
		if ( Request::instance()->isPost()) {
            $data = input();
            $result = $this->agentLogic->addEmployee($data);
            return $result;
		}
        // 获取所有代理商
        $agent = Db::name('agent')->where("is_use=1 and type=1")->field('id,agent_name')->select();

        $status = $this->agentLogic->getStatus();
        $this->assign('status', $status);
		$this->assign('id', '');
        $this->assign('info', []);
        $this->assign('all_image', json_encode([]));
        $this->assign('action', 'add_employee');
        $this->assign('agent',$agent);
		return $this->fetch();
	}

    // 上传图片
    public function file_upload(){
        $fileObj = new Upload();
        $dstPath = 'operator/Agent/';
        $res = $fileObj->upload('img_file', $dstPath, 1024*1024*10);
        if ($res['code'] == 0) {
            return json($res['msg']);
        } else {
            return json($res['msg']);
        }
	}


}